package cn.limeizi.ssm.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.limeizi.ssm.po.Tv;
import cn.limeizi.ssm.service.TvService;

@Controller
@RequestMapping("/admin")
public class AdminTvController {
	
	@Autowired
	private TvService tvService;
	
	@RequestMapping(value="/adminTv.action",method=RequestMethod.GET)
	public String index(Model model){
		
		List<Tv> tvList = tvService.selectAll();
		
		model.addAttribute("tvList", tvList);
		
		return "admin/tvs/tvList";
		
	}
	
	@RequestMapping(value="/toEditTv.action",method=RequestMethod.GET)
	public String toEdit(Model model,Integer id){
		
		if(id!=null){
			
			Tv tv=tvService.selectById(id);
			
			model.addAttribute("tv", tv);
		}
				
		return "admin/tvs/edit";
		
	}
	
	@RequestMapping(value="/editTv.action",method=RequestMethod.POST)
	public String edit(Tv tv){
		
		if(tv.getId()!=null){
			tvService.update(tv);
		}else{
			tvService.insert(tv);
		}
		
		return "redirect:adminTv.action";
	}
	
	@RequestMapping(value="/deleteTv.action",method=RequestMethod.GET)
	public String delete(Integer id){
		
		tvService.delete(id);;
		return "redirect:adminTv.action";
	}
}
